package util.ip;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IpReg {
	
	Pattern p1;
	Pattern p2;
	Pattern p3;
	Pattern p4;
	Pattern p5;
	Pattern p6;
	Pattern p7;
	
	Matcher m1;
	Matcher m2;
	Matcher m3;
	Matcher m4;
	Matcher m5;
	Matcher m6;
	Matcher m7;
	
	boolean b1;
	boolean b2;
	boolean b3;
	boolean b4;
	boolean b5;
	boolean b6;
	boolean b7;
	
	Set<String> removeIp = new HashSet<String>();
	
	
	public IpReg() {
		
		removeIp.add("0.0.0.0");
		removeIp.add("255.255.255.255");
		removeIp.add("127.0.0.1");
		
		//10.0.0.0－10.255.255.255
		p1 = Pattern.compile("^10(\\.([2][0-4]\\d|[2][5][0-5]|[01]?\\d?\\d)){3}$");
		//172.16.0.0－172.31.255.255
		p2 = Pattern.compile("^172\\.([1][6-9]|[2]\\d|3[01])(\\.([2][0-4]\\d|[2][5][0-5]|[01]?\\d?\\d)){2}$");
		//192.168.0.0－192.168.255.255
		p3 = Pattern.compile("^192\\.168(\\.([2][0-4]\\d|[2][5][0-5]|[01]?\\d?\\d)){2}$");
		
		//100.64.x.x－100.127.x.x
		p4 = Pattern.compile("^100\\.([6][4-9]|[7]\\d|[8]\\d|[9]\\d|[1][0-2][0-7])(\\.([2][0-4]\\d|[2][5][0-5]|[01]?\\d?\\d)){2}$");
		//224.0.0.0-239.255.255.255
		p5 = Pattern.compile("^2[2-3]\\d(\\.([2][0-4]\\d|[2][5][0-5]|[01]?\\d?\\d)){3}$");
		//169.254.x.x
		p6 = Pattern.compile("^169\\.254(\\.([2][0-4]\\d|[2][5][0-5]|[01]?\\d?\\d)){2}$");
		
		//p7 = Pattern.compile("^\\d{1,}+(\\.([2][0-4]\\d|[2][5][0-5]|[01]?\\d?\\d)){3}$");
		
	}
	
	public void setSrcPattern (String regex) {
		p7 = Pattern.compile(regex);
	}
	
	
	public boolean isP1(String ip) {
		m1 = p1.matcher(ip);
		return m1.matches();
	}
	public boolean isP2(String ip) {
		m2 = p2.matcher(ip);
		return m2.matches();
	}
	
	public boolean isP3(String ip) {
		m3 = p3.matcher(ip);
		return m3.matches();
	}
	public boolean isP4(String ip) {
		m4 = p4.matcher(ip);
		return m4.matches();
	}
	
	public boolean isP5(String ip) {
		m5 = p5.matcher(ip);
		return m5.matches();
	}
	
	
	public boolean isKeepIp(String ip) {
		if (removeIp.contains(ip)) {
			return true;
		}
		return false;
	}
	
	
	
	public boolean isUserSrc(String ip) {
		m7 = p7.matcher(ip);
		b7 = m7.matches();
		return b7;
	}

	
	public boolean isISP(String ip) {
	
		m1 = p1.matcher(ip);
		m2 = p2.matcher(ip);
		m3 = p3.matcher(ip);
		m4 = p4.matcher(ip);
		m5 = p5.matcher(ip);
		m6 = p6.matcher(ip);
		b1 = m1.matches();
		b2 = m2.matches();
		b3 = m3.matches();
		b4 = m4.matches();
		b5 = m5.matches();
		b6 = m6.matches();
		
		return !(b1 || b2 || b3 || b4 || b5 || b6 || isKeepIp(ip) 
				|| ip.equals("Broadcast") || ip.endsWith(".local") || ip.startsWith("IPv"));

		
		
	}

}
